<?php

class GalactusViewUser extends GalactusViewGallery {

	function __construct($control) {
		GalactusView::__construct($control);
	}

	public function retrieveCSS( ) {
		return '
			.schedule-area {
				overflow: auto;
			}

			.schedule-area + .post-area {
				margin-left: 0 !important;
			}

			.table-schedule td {
				text-align: center !important;
			}

			.table-schedule {
				margin-bottom: 0 !important;
			}
		';
	}

	public function retrieveJS( ) {
		return '
			function galactus_view_user_show_administrator_area( ) {
				$(".belongs-adm").removeClass("hide");
			}

			function galactus_view_user_show_user_area( ) {
				$(".content-area").resize(function( ) {
					$(".schedule-area").height(0.3*HEIGHT);
					$(".post-area").height(0.3*HEIGHT);
					$(this).height($(".schedule-area").parent( ).height( ) + $(".post-area").parent( ).height( ) - 20);
				});
				$(".content-area").trigger("resize");
				$(".nav-pills a").click(function( ) {
					var cmd = "'.URL.'" + $(this).attr("galactus-command");
					$("#contentarea").html("");
					$("#contentarea").load(cmd);
				});
				$(".content-area li").click(function( ) {
					$("#modal-content .item[content-id=\'" + $(this).attr("content-id") + "\']").addClass("active");
					$("#modal-content").modal("show");
				});
				$("#modal-content .btn").click(function( ) {
					switch($(this).attr("galactus-command")) {
						case "GALLERY/DOWNLOAD":
							var img = $("#modal-content .active img").attr("src").replace(/^data:image\/[^;]/, "data:application/octet-stream");
							window.open(img);
							break;
						case "GALLERY/REMOVE_CONTENT":
							var cmd = "'.URL.'" + $(this).attr("galactus-command");
							var content_id = $("#modal-content .active").attr("content-id");
							$("#contentarea").html("");
							$("#contentarea").load(cmd, { params: content_id });
							break;
					}
				});
			}

			function galactus_view_user_show_default_area( ) {
				$(".post-area").resize(function( ) {
					$(this).height(HEIGHT - $(".post-area").position( ).top - 80);
				});
				$(".post-area").trigger("resize");
			}
		';
	}

	public function show($command) {
		switch($command[2]) {
			case "CHANGE_PASSWORD":
				$this->showChangePassword( );
				break;
			case "EDIT":
				$this->showEdit( );
				break;
			default:
				switch($this->control->profile_id) {
					case ADMINISTRATOR: $this->showAdministratorArea( ); break;
					case USER: $this->showUserArea( ); break;
					default: $this->showDefaultArea( ); break;
				}
				break;
		}
	}

	private function showChangePassword( ) {
		echo '
			<form method="post" action="'.URL.'USER/CHANGE_PASSWORD" class="form-horizontal offset2">
				<div class="control-group">
					<label class="control-label" for="password">Nova senha</label>
					<div class="controls">
						<input type="password" name="params[ ]" class="span4" id="password" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="password-confirm">Confirmação de senha</label>
					<div class="controls">
						<input type="password" name="params[ ]" class="span4" id="password-confirm" />
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="reset" class="btn span2">Limpar</button>
						<button type="submit" class="btn btn-primary span2">Confirmar</button>
					</div>
				</div>
			</form>
		';
	}

	private function showEdit( ) {
		$user = $this->control->model->retrieveUser($this->control->user_id);
		echo '
			<form method="post" action="'.URL.'USER/EDIT" class="form-horizontal offset2">
				<div class="control-group">
					<label class="control-label" for="name">Nome</label>
					<div class="controls">
						<input type="text" name="params[ ]" class="span4" id="name" value="'.$user[0]["name"].'" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="email">E-mail</label>
					<div class="controls">
						<input type="email" name="params[ ]" class="span4" id="email" value="'.$user[0]["email"].'" />
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="reset" class="btn span2">Limpar</button>
						<button type="submit" class="btn btn-primary span2">Confirmar</button>
					</div>
				</div>
			</form>
		';
	}

	private function showAdministratorArea( ) {
		$this->showUserArea( );
		echo '<script type="text/javascript">$(galactus_view_user_show_administrator_area( ));</script>';
	}

	private function showUserArea( ) {
		echo '
			<div class="container-fluid">
				<div class="row-fluid">
					<ul class="nav nav-pills pull-right">
						<li><a galactus-command="USER/SHOW/CHANGE_PASSWORD">Alterar Senha</a></li>
						<li><a galactus-command="USER/SHOW/EDIT">Editar</a></li>
						<li><a galactus-command="USER/REMOVE">Remover</a></li>
						<li><a galactus-command="USER/SIGN_OUT">Sair</a></li>
					</ul>
				</div>
				<div class="row-fluid">
					<div class="span5">
						<div class="row-fluid"><h4>.: Horários Agendados</h4></div>
						<div class="row-fluid"><div class="schedule-area span12 well">'.$this->retrieveSchedule( ).'</div></div>
						<div class="row-fluid"><h4>.: Recados</h4></div>
						<div class="row-fluid"><div class="post-area span12 well">'.$this->retrievePosts( ).'</div></div>
					</div>
					<div class="span7">
						<div class="row-fluid"><h4>.: Minha Galeria</h4></div>
						<div class="row-fluid"><div class="content-area span12 well">'.$this->retrieveGallery($this->control->user_id).'</div></div>
					</div>
				</div>
			</div>
			'.$this->retrieveCarousel( ).'
			<script type="text/javascript">$(galactus_view_user_show_user_area( ));</script>
		';
	}

	private function showDefaultArea( ) {
		echo '
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="span4">
						<form method="post" action="'.URL.'USER/SIGN_IN" class="well">
							<div class="controls controls-row">
								<input type="email" placeholder="E-mail" name="params[ ]" class="span12" />
							</div>
							<div class="controls controls-row">
								<input type="password" placeholder="Senha" name="params[ ]" class="span8" />
								<button type="submit" class="btn btn-primary span4">Entrar</button>
							</div>
						</form>
						<div>Quer controlar um telescópio? <strong>Inscreva-se!</strong></div>
						<form method="post" action="'.URL.'USER/SIGN_UP" class="well">
							<div class="controls controls-row">
								<input type="text" placeholder="Nome" name="params[ ]" class="span12" />
							</div>
							<div class="controls controls-row">
								<input type="email" placeholder="E-mail" name="params[ ]" class="span12" />
							</div>
							<div class="controls controls-row">
								<input type="password" placeholder="Senha" name="params[ ]" class="span12" />
							</div>
							<div class="controls controls-row">
								<button type="submit" class="btn btn-primary span12">Inscrever-se</button>
							</div>
						</form>
					</div>
					<div class="post-area span8 well"><ul class="thumbnails post-items">'.$this->retrievePosts( ).'</ul></div>
				</div>
			</div>
			<script type="text/javascript">$(galactus_view_user_show_default_area( ));</script>
		';
	}

	public function retrieveSchedule( ) {
		$schedule = $this->control->model->retrieveSchedule(NULL, $this->control->user_id);
		if(empty($schedule))
			return '
				<div class="alert">
					<button type="button" class="close" data-dismiss="alert">&times;</button>
					<strong>Agenda:</strong> Nenhum agendamento realizado!
				</div>
			';
		$content = "";
		for($i = 0; $i < count($schedule); ++$i) {
			$telescope = reset($this->control->model->retrieveTelescope($schedule[$i]["telescope_id"]));
			$table = '
				<table class="table table-condensed table-schedule">
					<thead>
						<tr><th colspan="2">Telescópio: '.$telescope["name"].'</th></tr>
						<tr><th><i class="icon-time"></i> Início</th><th><i class="icon-time"></i> Fim</th></tr>
					</thead>
					<tbody>
			';
			for($j = $i; $j < count($schedule); ++$j) {
				$date_start = new DateTime($schedule[$j]["date_start"]);
				$date_end = new DateTime($schedule[$j]["date_end"]);
				if($schedule[$i]["telescope_id"] == $schedule[$j]["telescope_id"]) {
					$table .= '
						<tr>
							<td class="text-info">'.$date_start->format("d/m/Y H\hi").'</td>
							<td class="text-info">'.$date_end->format("d/m/Y H\hi").'</td>
						</tr>
					';
				}
				else break;
				$i = $j;
			}
			$table .= '</tbody></table>';
			$content .= '
				<li class="thumbnail span12">'.$table.'</li>
			';			
		}
		return '<ul class="thumbnails post-items">'.$content.'</ul>';
	}

}
?>
